import Vue from 'vue'
import App from './App.vue'

//引用组件库

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);


import Vant from 'vant';
//引入所有组件
import 'vant/lib/index.css';
//引入路由器
import VueRouter from 'vue-router'
import router from './router/index.js'
import Vuex from 'vuex';
import vueResource from 'vue-resource';
//引入全局的less
import '../src/assets/common/index.less'
import 'lib-flexible/flexible.js'



//测试
import request from '../request'
Vue.prototype.$request = request
// this.$request('/login')



import axios from 'axios'

//请求路径
axios.defaults.baseURL = "http://www.zhangminghui.vip:8080"

//懒加载
import { Lazyload } from 'vant';
Vue.use(Lazyload, {
  lazyComponent: true,
});

import { Area } from 'vant';

import store from '../store/index'
//引入全局的ws
import global from './ws.js'

Vue.prototype.global = global


//关闭vue的生产提示
Vue.config.productionTip = false


Vue.config.devtools = true;

//应用
Vue.use(Area);
Vue.use(Vant);
Vue.use(Vuex)
Vue.use(vueResource)
Vue.use(VueRouter)




// 定义全局默认配置(统一将token加入header请求头中)
axios.defaults.headers.common['Authentication-Token'] = sessionStorage.getItem("token");

// 页面刷新时，重新赋值token
if (sessionStorage.getItem('token')) {
  store.commit('setToken', sessionStorage.getItem('token'))
}



// 请求拦截器
// axios.interceptors.request.use(config=>{
//   // let needLoginUrl = ['/body/sendMessage','/chat/getChatList','/report/reportAUser','/chat/getChatList'] //数组里面放需要登录的接口地址
//   let needLoginUrl = [] //数组里面放需要登录的接口地址
//   // let needLoginUrl = ['/body/sendMessage','/body/getSuchMessage','/body/sendMessage','/chat/getChatList','/chat/getChatRecords','/chat/clearSystemMsgUnread','/chat/getSystemChatRecords','/chat/getSystemMsgUnread','/chat/inWindows','/chat/isFirstChat','/chat/resetWindows','/chat/unread'] //数组里面放需要登录的接口地址
//   // console.log(config.url)
//   // console.log(config)
//   let baseUrl = config.url.indexOf('?')!== -1 ? config.url.split('?')[0] : config.url
//   // console.log(needLoginUrl.indexOf(baseUrl))//判断当前请求的地址是否需要登录
//   // console.log(baseUrl)
//   if(needLoginUrl.indexOf(baseUrl) !== -1){
//     // config.headers.common['Authentication-Token'] = JSON.parse(sessionStorage.getItem('token'))
//     console.log(111)
//   }else{
//     config.headers.common['Authentication-Token'] = ''
//   }
//   return config
// },function(err){
//   return Promise.reject(err)
// })









new Vue({
  el: '#app',
  render: h => h(App),
  router: router,
  store,
  // beforeCreate(){
  //   Vue.prototype.$bus = this
  // }
})



// 添加请求拦截器
// Axios.interceptors.request.use(config => {
//   // 在发送请求之前
//   const needTokeUrls = ['/body/getSuchMessage']
//   let url =config.url;
//   console.log(store)
//   if(needTokeUrls.includes(url) && store.state.token){
//     console.log('添加')
//     config.headers.common['Authentication-Token'] = store.state.token
//   }else{
//     console.log('取消token')
//     config.headers.common['Authentication-Token'] = ''
//   }
//   //判断是否存在token，如果存在将每个页面header都添加token
//   // if (store.state.token) {
//   //   config.headers.common['Authentication-Token'] = store.state.token
//   // }
//   // config.headers.common['Authentication-Token'] = ''
//   return config;
// }, error => {
//   // 对请求错误做些什么
//   return Promise.reject(error);
// });


// 拦截请求
// axios.interceptors.request.use(config => {  // 请求头添加access_token
//   const token = sessionStorage.getItem('token') ? JSON.parse(sessionStorage.getItem('token')) : ''
//   if (token) {
//     config.headers['Authentication-Token'] = token
//   }
//   console.log(config)
//   return config
// }, error => {
//   console.log(error.message)
// })






// http response 拦截器
// Axios.interceptors.response.use(
//   response => {

//     return response;
//   },
//   error => {

//     if (error.response) {
//       switch (error.response.status) {
//         case 401:
//           this.$store.commit('delToken');
//           router.replace({
//             path: '/login',
//             query: { redirect: router.currentRoute.fullPath }//登录成功后跳入浏览的当前页面
//           })
//       }
//     }
//     return Promise.reject(error.response.data)
//   });


